const StatusOk = 200;

//没有用到jquery的Ajax
//将xmlHttpResquest对象方法封装起来，
var createXhr = function () {
  var xhr = null;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else {
    xhr = new ActiveXObject("Microsoft.XMLHtTP");
  }
  return xhr;
};

//下拉框级联查询---查省份询
var provinceSel = document.getElementById("province");

var xhr = createXhr();
if (xhr) {
  xhr.open("GET", "http://192.168.152.35/province");
  xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function (data) {
    //省份下的城市
    if (xhr.readyState == 4 && xhr.status == StatusOk) {
      var arr = JSON.parse(xhr.responseText);
      for (var i = 0; i < arr.length; i++) {
        var province = arr[i];
        var id = province.id;
        var name = province.name;
        //创建下拉框中的<option></option>
        var opt = new Option(name, id);
        //加到<select>中
        provinceSel.add(opt);
      }
    }
  };
}
xhr.send();

//下拉框级联查询---查询城市
function queryCity() {
  //获取省份 pid
  var pid = document.getElementById("province").value;
  var citySel = document.getElementById("city");
  //清空上次结果
  citySel.length = 0;
  alert("pid:" + pid);
  var xhr = createXhr();
  if (xhr) {
    xhr.open("POST", "192.168.152.35:4000/queryCity");
    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function (data) {
      //省份下的城市
      if (xhr.readyState == 4 && xhr.status == StatusOk) {
        alert(data);
        var arr = JSON.parse(xhr.responseText);
        for (var i = 0; i < arr.length; i++) {
          var city = arr[i];
          var id = city.id;
          var name = city.name;
          //创建下拉框中的<option></option>
          var opt = new Option(name, id);
          //加到<select>中
          citySel.add(opt);
        }
      }
    };
  }
  //请求写入， DataForm,并发送
  xhr.send("pid=", pid);
}

//下拉框级联查询---查询景点
function queryViewport() {
  //获取 cid
  var pid = document.getElementById("province").value;
  var viewSel = document.getElementById("viewport");
  //清空上次结果
  viewSel.length = 0;
  // alert('pid:' + pid);
  var xhr = createXhr();
  if (xhr) {
    xhr.open("GET", "http://192.168.152.35/attractions?pid=" + pid);
    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function (data) {
      //
      if (xhr.readyState == 4 && xhr.status == StatusOk) {
        // alert(data);
        var arr = JSON.parse(xhr.responseText);
        for (var i = 0; i < arr.length; i++) {
          var viewport = arr[i];
          var id = viewport.id;
          var name = viewport.name;
          //创建下拉框中的<option></option>
          var opt = new Option(name, id);
          //加到<select>中
          viewSel.add(opt);
        }
      }
    };
  }
  xhr.send();
}

//旅游热度---map_base       套餐人数

$(function () {
  var chart = echarts.init(document.getElementById("map"), "white", {
    renderer: "canvas",
  });
  $.ajax({
    type: "GET",
    url: "http://192.168.152.35/travlenums",
    dataType: "json",
    success: function (result) {
      chart.setOption(result);
    },
  });
});

//景点分析折线图
// 方案一 接收配置项
// function viewportAnalyse() {
//   var vid = document.getElementById("viewport").value;
//   // alert('vid:'+vid);
//   //景点分析---line_color_with_js_func   （评分、价格）
//   $(function () {
//     var chart = echarts.init(
//       document.getElementById("line_color_with_js_func"),
//       "white",
//       { renderer: "canvas" }
//     );
//     $.ajax({
//       type: "GET",
//       url: "http://192.168.152.35:5000/attractionline",
//       // url: "http://192.168.152.35/attractionline?vid=" + vid,
//       dataType: "json",
//       success: function (result) {
//         console.log(result);
//         chart.setOption(result);
//       },
//     });
//   });

  
// }


//方案二：修改 iframe
function viewportAnalyse() {
  var vid = document.getElementById("viewport").value;
  $("#iframe_line_color_with_js_func").attr('src',"http://192.168.152.35/attractionline?vid="+vid);   
  var src = $("#iframe_line_color_with_js_func").attr('src');   
  // alert("src:"+src)
  
}

// 获取景点简介

$("#viewport_brief_introduction").click(function () {
  var vid = $("#viewport").val();
  // alert(vid);
  $.ajax({
    url: "http://192.168.152.35/brief?vid="+vid,
    type: "GET",
    dataType: "json",
    success: function (data) {//接收格式[{"title":"","data_content":""}]
      // alert("adata:"+data)
      // alert("adata[0]:"+data[0])
      // alert("adata.title:"+data[0].title)
 
      // alert("title:"+title+"content:"+data_content);
      //  alert("length:"+data.length);

      //初始化，防止脏数据
       var str ="";
       $(".h5text").remove();
       $(".spantext").remove();
       $(".modal-body br").remove();
      for(var i =0;i<data.length;i++){
        var title = data[i].title;
        var data_content = data[i].data_content;
        
        // $("#viewport_brief_introduction").delay(3200).attr({
        //   "title": title,
        //   "data-original-title": title,
        //   "data-content": data_content,
        // });
        
        str+="<h5 class='h5text'>"+title+"</h5>"+"<span class='spantext'>"+data_content+"</span>"+"<br>"
      }
      // alert(str);
      $(".modal-body").append(str);
     
    },
    error: function () {
      alert("景点简介获取异常！");
    },
  });

  
  //
  //   alert($("#viewport_brief_introduction").attr('title'))
  //   alert($("#viewport_brief_introduction").attr('data-content'))
});

//套餐分析---basic_wordcloud
$("#inputGroupSelect04Button").click(function () {
  var chart = echarts.init(
    document.getElementById("basic_wordcloud"),
    "white",
    { renderer: "canvas" }
  );
  var star = $("#inputGroupSelect04").val();
  $.ajax({
    type: "GET",
    url: "http://192.168.152.35/comment?score="+star,
    dataType: "json",
    success: function (result) {
      chart.setOption(result);
    },
    error:function(c){
      alert("词云数据异常！");
    }
  });
});
